package com.linq.cool.beauty.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.linq.cool.beauty.domain.BeautyOrder;
import com.linq.cool.beauty.domain.BeautyOrderItem;
import com.linq.cool.beauty.dto.BeautyOrderDTO;
import com.linq.cool.beauty.dto.BeautyOrderQueryDTO;
import com.linq.cool.beauty.service.BeautyOrderItemService;
import com.linq.cool.beauty.service.BeautyOrderService;
import com.linq.cool.common.constants.UserConstants;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/2/11 01:02
 * @Description:
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("order")
public class BeautyOrderController extends BaseController {
    @Autowired
    private BeautyOrderService beautyOrderService;
    @Autowired
    private BeautyOrderItemService beautyOrderItemService;

    @PreAuthorize(hasPerm = "beauty:order:list")
    @GetMapping("list")
    public Result<?> list(BeautyOrderQueryDTO dto) {
        IPage<BeautyOrder> page = beautyOrderService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    /**
     * 创建订单
     *
     * @param dto
     *
     * @return
     */
    @PostMapping("createOrder")
    public Result<?> createOrder(@Validated @RequestBody BeautyOrderDTO dto) {
        return Result.ok(beautyOrderService.createOrder(dto));
    }

    /**
     * 分页获取客户的所有订单信息
     *
     * @return
     */
    @GetMapping("getMemberOrders")
    public Result<?> getMemberOrders(BeautyOrderQueryDTO dto) {
        IPage<BeautyOrder> page = beautyOrderService.listMemberOrdersByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    /**
     * 客户取消订单
     * 只有待付款的才能取消
     * 其他 的取消需要通过后台
     *
     * @param orderId 订单id
     *
     * @return
     */
    @PutMapping("cancelMemberOrder/{orderId}")
    public Result<?> cancelMemberOrder(@PathVariable("orderId") Long orderId) {
        beautyOrderService.cancelMemberOrder(orderId);
        return Result.ok("取消订单成功");
    }

    /**
     * 查询当前客户的所有订单项
     *
     * @param orderId
     *
     * @return
     */
    @GetMapping("getMemberOrderItems/{orderId}")
    public Result<?> getMemberOrderItems(@PathVariable("orderId") Long orderId) {
        return Result.ok(beautyOrderService.getMemberOrderItems(orderId));
    }

    /**
     * 订单确认付款 前一个状态是 待付款
     *
     * @param orderId
     *
     * @return
     */
    @PostMapping("confirmMemberOrderPay/{orderId}")
    public Result<?> confirmMemberOrderPay(@PathVariable("orderId") Long orderId) {
        beautyOrderService.confirmMemberOrderPay(orderId);
        return Result.ok();
    }

    /**
     * 查询客户当前订单信息
     *
     * @param orderId
     *
     * @return
     */
    @GetMapping("getMemberOrder/{orderId}")
    public Result<?> getMemberOrder(@PathVariable("orderId") Long orderId) {
        BeautyOrder one = beautyOrderService.getOne(new LambdaQueryWrapper<BeautyOrder>()
                                                            .eq(BeautyOrder::getId, orderId)
                                                            .eq(BeautyOrder::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                            .eq(BeautyOrder::getMemberId, SecurityUtils.getUserId())
        );
        if (null == one) {
            return Result.fail("该订单不存在");
        }
        return Result.ok(one);
    }

    /**
     * 客户确认已经服务完毕 上一个状态待确认
     *
     * @param orderId
     *
     * @return
     */
    @PutMapping("confirmMemberOrder/{orderId}")
    public Result<?> confirmMemberOrder(@PathVariable("orderId") Long orderId) {
        beautyOrderService.confirmMemberOrder(orderId);
        return Result.ok("确认已服务完毕成功");
    }

    /**
     * 获取客户未评价的订单项
     *
     * @param orderId
     *
     * @return
     */
    @GetMapping("getNotEvaluationMemberOrderItems/{orderId}")
    public Result<?> getNotEvaluationMemberOrderItems(@PathVariable("orderId") Long orderId) {
        BeautyOrder one = beautyOrderService.getOne(new LambdaQueryWrapper<BeautyOrder>()
                                                            .eq(BeautyOrder::getId, orderId)
                                                            .eq(BeautyOrder::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                            .eq(BeautyOrder::getMemberId, SecurityUtils.getUserId())
        );
        if (null == one) {
            return Result.fail("该订单不存在");
        }
        List<BeautyOrderItem> orderItems = beautyOrderItemService.list(new LambdaQueryWrapper<BeautyOrderItem>()
                                                                               .eq(BeautyOrderItem::getOrderId, orderId)
                                                                               .eq(BeautyOrderItem::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                                               .eq(BeautyOrderItem::getIsEvaluation, UserConstants.StateEnum.ORDER_EVALUATION_STATUS_0.getCode())
        );
        if (CollectionUtils.isEmpty(orderItems)) {
            orderItems = new ArrayList<>(0);
        }
        return Result.ok(orderItems);
    }
}
